import requests
from prettytable import *
import random
import json
import mysql


def get_url(url, params=None, proxies=None):
    user_agent_list = ["Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
                "Mozilla/5.0 (Windows NT 10.0;) Gecko/20100101 Firefox/61.0",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
                "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15",
                ]

    random_agent = random.choice(user_agent_list)
    # print(random_agent)

    header = {
        'User-Agent' : random_agent,
        'Referer' : 'http://fundf10.eastmoney.com/'
    }
    # print(params)
    rsp = requests.get(url, params=params, proxies=proxies, headers=header)
    rsp.encoding = "utf-8"
    rsp.raise_for_status()
    return rsp.text


def get_fund_data(code, start='', end=''):
    record = {'Code': code}
    url = 'http://api.fund.eastmoney.com/f10/lsjz'
    params = {'fundCode': code, 'pageIndex': 1, 'pageSize': 1, 'startDate': start, 'endDate': end, '_': random.random()}
    json_str = get_url(url, params)
    # print(json_str)
    json_obj = json.loads(json_str)

    records = []

    # 判断是否有返回数据
    if not json_obj['Data']:
        return records

    for o in json_obj['Data']['LSJZList']:
        record['Date'] = o['FSRQ']
        record['NetAssetValue'] = o['DWJZ']
        record['ChangePercent'] = o['JZZZL']
        if o['JZZZL']=="":
            continue
        records.append(record.copy())

    return records


# def demo(code, start, end):
#     table = PrettyTable()
#     table.field_names = ['Code', 'Date', 'NAV', 'Change']
#     table.align['Change'] = 'r'
#     records = get_fund_data(code, start, end)
    
    # for record in records:
    #     table.add_row([record['Code'], record['Date'], record['NetAssetValue'], record['ChangePercent']])
    # return table



if __name__ == "__main__":

    # yesterday = (datetime.datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
    yesterday = "2021-03-01"

    # Load All Fund's Code
    fund_arr = mysql.getDiyFunds("SELECT fundId FROM jjjz WHERE FSRQ = '%s' AND DWJZ IS NULL" % (yesterday))

    for fund in fund_arr:
        fundId = fund['Code']
        records = get_fund_data(fundId, yesterday, yesterday)
        if len(records) > 0:
            # print("更新：%s" % (fundId))
            mysql.updateDayFundValue(fundId, records)
        # time.sleep(1)

    print("%s日期基金净值抓取完成" % (yesterday))



